<template>
  <div class="app-container" style="margin-top: 30px;margin-left: 20px;margin-right: 20px;">
    <div class="filter-container" style="margin-bottom: 20px;">
      <i :class="[refresh? 'el-icon-refresh go' : 'el-icon-refresh']" style="margin-left: 10px;" @click="iconClick" />
      <el-input v-model="listQuery.parameter" placeholder="姓名" style="width: 200px;margin-left: 10px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        添加
      </el-button>
    </div>

   <!-- <aside style="color: gray; font-size: medium;margin-bottom: 5px;">*老人信息管理</aside> -->
    <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="list.slice((currentPage-1)*pageSize,currentPage*pageSize)"
        border
        fit
        highlight-current-row
        style="width: 100%;"
    >
      <el-table-column label="id" prop="id" sortable="custom" align="center" width="100" :class-name="getSortClass('id')">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" width="50px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.sex }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年龄" width="50px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.age }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" width="150px" align="center">
        <template slot-scope="{row}">
          <el-tag>{{ row.type | typeFilter1}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="采集状态" width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.hasCaptured | typeFilter2}}</span>
        </template>
      </el-table-column>
      <el-table-column label="人像采集" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="{row}">
          <el-button :class="row.hasCaptured === true ? 'gray-button' : 'blue-button'" :disabled="row.hasCaptured === true" size="mini" @click="handleVideo(row)">
            开始采集
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="Actions" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            修改
          </el-button>
          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDelete(row,$index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        align="center"
        :current-page="currentPage"
        :page-sizes="[1,5,10,20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="list.length"
        style="width: 90% ;margin-top:20px;"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
   <!-- <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" /> -->
    <el-dialog :visible.sync="dialogPvVisible" title="FORM">
      <el-form ref="dataForm" :model="temp" label-position="left" label-width="100px" style="width: 400px; margin-left:80px;">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-input v-model="temp.sex" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="temp.age" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="temp.type" class="filter-item" placeholder="选择">
            <el-option v-for="item in TypeOptions1" :key="item.key" :label="item.display_name" :value="item.key" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogPvVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          提交
        </el-button>
      </div>
    </el-dialog>
    <el-dialog title='人脸采集' :visible.sync="visible" :before-close="close">
      <img id="video" :src="source" width="100%" height="400px" >
    </el-dialog>
  </div>
</template>

<script>
import {getWorkerInfo, addOneWorker, deOneWorker, updateOneWorker, oneWorkerInfo} from "@/api/infoApi";
const TypeOptions1 = [
  { key: 0, display_name: '普通员工' },
  { key: 1, display_name: '义工' }
]
const TypeOptions2 = [
  { key: 'false', display_name: '未完成' },
  { key: 'true', display_name: '已完成' }
]
const TypeKeyValue1 = TypeOptions1.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})
const TypeKeyValue2 = TypeOptions2.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})

export default {
  name: 'WorkerInfo',
  filters: {
    typeFilter1(type) {
      return TypeKeyValue1[type]
    },
    typeFilter2(type) {
      return TypeKeyValue2[type]
    }
  },
  data() {
    return {
      TypeOptions1,
      source:"",
      visible:false,
      refresh:false,
      tableKey: 0,
      list: null,
      listLoading: false,
      listQuery: {
        importance: undefined,
        parameter: undefined
      },
      importanceOptions: ['姓名'],
      temp: {
        name: '',
        sex: '',
        age: '',
        type: '',
        hasCaptured: 0
      },
      dialogStatus: '',
      dialogPvVisible: false,
      currentPage: 1, // 当前页码
      total: 0, // 总条数
      pageSize: 10 // 每页的数据条数
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      getWorkerInfo({}).then(res => {
      console.log(res);
      this.list = res.result;
    }).catch(err => {
      console.log(err);
    })
    },
    iconClick() {
      this.refresh = !this.refresh
      this.getList();
      this.listQuery = {importance: undefined,parameter: undefined};
      setTimeout(() => {
        this.refresh = !this.refresh
      }, 1000)
    },
    resetTemp() {
      this.temp = {
        name: '',
        sex: '',
        age: '',
        type: '',
        hasCaptured: 0
      }
    },
    handleFilter() {
      if (this.listQuery.parameter === '') {
        this.getList()
      } else {
        oneWorkerInfo(this.listQuery.parameter).then(response => {
          console.log(response)
          this.list = response.result
          this.listLoading = false
        }).catch(error => {
          console.log(error)
        })
      }
    },

    handleCreate() {
      this.resetTemp()
      this.dialogPvVisible = true
      this.dialogStatus = 'create'
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      var tempData = Object.assign({}, this.temp)
      addOneWorker(tempData).then(response => {
        console.log(response)
        this.getList()
        this.dialogPvVisible = false
      }).catch(error => {
        console.log(error)
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogPvVisible = true
      this.dialogStatus = 'update'
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      var tempData = Object.assign({}, this.temp)
      updateOneWorker(tempData).then(response => {
        console.log(response)
        this.getList()
        this.dialogPvVisible = false
      }).catch(error => {
        console.log(error)
      })
    },
    handleDelete(row, index) {
      console.log(row.id)
      deOneWorker({ id: row.id }).then(response => {
        console.log(response)
        this.getList()
        this.listLoading = false
      }).catch(error => {
        console.log(error)
      })
      this.$notify({
        title: 'Success',
        message: 'Delete Successfully',
        type: 'success',
        duration: 2000
      })
      this.list.splice(index, 1)
    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    },
    handleVideo(row) {
      console.log(row)
      this.source = 'http://127.0.0.1:5000/worker/collecting_faces?id='+row.id;
      this.visible = true;
    },
    close(){
      this.source = ''
      this.visible = false;
      this.getList()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.currentPage = 1
      this.pageSize = val
    },
    // 当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
    }
  }
}
</script>
<style scoped>
.blue-button {
  background-color: rgb(191, 210, 249);
  /* 其他样式属性 */
}

.gray-button {
  background-color: gray;
  /* 其他样式属性 */
}
</style>
